package com.hualan.sfzy.service.mind.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.mind.MindTestClassify;
import com.hualan.sfzy.mapper.mind.MindTestClassifyMapper;
import com.hualan.sfzy.service.BaseServiceImp;
import com.hualan.sfzy.service.mind.MindTestClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MindTestClassifyServiceImpl extends BaseServiceImp<MindTestClassify> implements MindTestClassifyService {
    @Autowired
    private MindTestClassifyMapper mindTestClassifyMapper;

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, MindTestClassify mindTestClassify) {
        PageHelper.startPage(pageNum, pageSize);
        List<MindTestClassify> list = mindTestClassifyMapper.select(mindTestClassify);
        PageInfo<MindTestClassify> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    public int countTestsByClassifyId(Integer id) {
        return mindTestClassifyMapper.countTestsByClassifyId(id);
    }

    @Override
    @Transactional
    public int remove(Integer id) {
        int count = countTestsByClassifyId(id);
        if (count > 0) {
            throw new RuntimeException("有分类下课程无法删除");
        }
        return super.remove(id);
    }

    @Override
    @Transactional
    public int removeBatch(Integer[] ids) {
        for (Integer id : ids) {
            int count = countTestsByClassifyId(id);
            if (count > 0) {
                throw new RuntimeException("有分类下课程无法删除");
            }
        }
        return super.removeBatch(ids);
    }
}